﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECOSYSTEM_BL;
using ECOSYSTEM_Dominio;

namespace ECOSYSTEMPRESENTACION
{
    public partial class frmArea : Form
    {
        public frmArea()
        {
            InitializeComponent();
        }
        Area elArea = new Area();
        AreaBL elAreaBL = new AreaBL();
        private Area ObtenerObjetoAre() {
            elArea.Nombre = txtNombreArea.Text;
            elArea.Descripcion = txtDescripcionArea.Text;
            return elArea;
        }
        private Boolean GuardarArea() {
            Area elArea = ObtenerObjetoAre();
            elAreaBL.RegistrarArea(elArea);
            return true;
        }
        private Boolean ActualizarArea() {
            Area elArea = ObtenerObjetoAre();
            elAreaBL.ActualizarArea(elArea, Convert.ToInt32(lblCodigoArea.Text));
            return true;
        }             
        private void llenarGRILAArea() {
            if (dgvListaArea.DataSource == null) {
                dgvListaArea.DataSource = elAreaBL.ObtenerArea();
                filtrador.DataSource = elAreaBL.ObtenerArea();
            }
        }
        private void AplicarFiltro()
        {
            int ret = Filtrar_dgvListaMarcas("Area", txtareaNombreBuscar.Text.Trim(), filtrador, dgvListaArea);
            if (ret == 0)
            {
                txtareaNombreBuscar.BackColor = Color.Red;
            }
            else
            {
                txtareaNombreBuscar.BackColor = Color.White;
            }
        }
        private int Filtrar_dgvListaMarcas(String columna, String texto, BindingSource filtro, DataGridView data)
        {
            if (filtrador.DataSource == null) return 0;
            try
            {
                String fil = String.Empty;
                fil = "LIKE '" + texto.Trim() + "%'";
                if (fil != String.Empty)
                {
                    fil = "[" + columna + "]" + fil;
                }
                filtro.Filter = fil;
                data.DataSource = filtro.DataSource;
                return filtro.Count;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            return 0;
        }
        private void LimpiarCampos() {
            txtareaNombreBuscar.Text = "";
            dgvListaArea.DataSource = null;
            txtNombreArea.Text = "";
            txtDescripcionArea.Text = "";
            btnRegistrar.Enabled = true;
            btnActualizar.Enabled = false;
            btnCancelar.Enabled = false;
        }
        private void rbActulizarArea_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                groupBox2.Enabled = true;
                groupBox3.Enabled = false;
                btnRegistrar.Enabled = false;
                btnCancelar.Enabled = true;
                llenarGRILAArea();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void rbNuevoArea_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                groupBox2.Enabled = false;
                groupBox3.Enabled = true;
                LimpiarCampos();
                btnActualizar.Enabled = false;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnRegistrar_Click(object sender, EventArgs e)
        {
            try
            {
                GuardarArea();
                MessageBox.Show("Area Registrado Correctamente");
                LimpiarCampos();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnActualizar_Click(object sender, EventArgs e)
        {
            try
            {
                ActualizarArea();
                MessageBox.Show("Area ACTUALIZADA Correctamente");
                rbNuevoArea.Checked = true;
                rbActulizarArea.Checked = false;
                LimpiarCampos();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                rbNuevoArea.Checked = true;
                rbActulizarArea.Checked = false;
                groupBox3.Enabled = true;
                groupBox2.Enabled = false;
                LimpiarCampos();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txtareaNombreBuscar_TextChanged(object sender, EventArgs e)
        {
            AplicarFiltro();
        }

        private void dgvListaArea_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                Int32 nroFila = Int32.Parse(e.RowIndex.ToString());
                txtNombreArea.Text = Convert.ToString(dgvListaArea["Area", nroFila].Value);
                txtDescripcionArea.Text = Convert.ToString(dgvListaArea["Descripcion",nroFila].Value);
                lblCodigoArea.Text = Convert.ToString(dgvListaArea["Codigo",nroFila].Value);
                btnActualizar.Enabled = true;
                groupBox3.Enabled = true;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }                
        }

    }
}
